// pages/friendPage/friendPage.js
var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
friend:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
this.load_data()
  },

  onShow(){

  },

  load_data(){
    let that=this
    wx.cloud.callFunction({
      name:"getUserInfo",
      data:{
        openid:app.globalData.openid,
      },
      success(res){
         that.setData({
           friend:res.result.data[0]["friend"]
         })
         console.log(res.result.data[0]["friend"])
      },
      fail(res){
       console.log("数据拉取失败",res)
      }
    })
  },

  delfriend(e){
    wx.showLoading({
      title: '取消关注中...',
      mask:true
    })
    var friend_openid=e.currentTarget.dataset.openid
    var i=0
    for(let k in this.data.friend)
    { console.log(this.data.friend[k]['openid'])
      if(this.data.friend[k]['openid']==friend_openid)
           break
      i+=1
    }
    this.data.friend.splice(i-1,1)
    this.setData({
      friend:this.data.friend
    })

    wx.hideLoading()
          wx.showToast({
            title: '已取消关注',
            icon: 'success',
            duration: 1500
          })
          setTimeout(this.onShow, 1000)
  },
onUnload: function () {
  this.updateDB()
},

updateDB(){
  let that=this
  wx.cloud.database().collection('user_Info').where({
    openid:app.globalData.openid
  }).get({
    success(res){
      wx.cloud.database().collection('user_Info').doc(res.data[0]['_id']).update({
        data:{
          friend:that.data.friend
        }
      })
    }
  })
}

})